/* INCLUDES ******************************************************************/

#include "ses_timer.h"


/* DEFINES & MACROS **********************************************************/
// to configure the timer we need registers,  like TCRR2A and the others  ..
// TCR
#define TIMER2_INT_vect  		TIMER2_COMPA_vect
#define TIMER2_MODE_A			1<< WGM21 // | (1<< CMP...) if we want to initialize other options
#define TIMER2_MODE_B			0x04
#define TIMER2_INT_MASK			1<<OCIE2A
#define TIMER2_INT_FLAG         //
#define TIMER2_CYC_FOR_1MILLISEC	250 // after 1 mill sec an interrupt will be triggered

/*FUNCTION DEFINITION ********************************************************/

pTimerCallback callback;
void timer2_init(pTimerCallback cb) {
	TCNT2 = 0;
	// CTC
	TCCR2A =TIMER2_MODE_A;
	TCCR2B =TIMER2_MODE_B;
	OCR2A = TIMER2_CYC_FOR_1MILLISEC;
	TIMSK2=TIMER2_INT_MASK	;
	// the callback function will point on the function cb, each time an interrupt is triggered the callback() will be called
	callback = cb;
}

ISR(TIMER2_INT_vect) {
	callback();
}
